import { Component, OnInit, Input, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
import { Observable } from 'rxjs/Rx';

@Component({
  selector: 'app-change-couter',
  template: `
    <p>当前值：{{ counter }}</p>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class CounterComponent implements OnInit {
  counter: number = 0;

  @Input() addStream: Observable<any>;

  constructor(private cdRef: ChangeDetectorRef) {}

  ngOnInit() {
    this.addStream.subscribe(() => {
      this.counter++;
      // 每次subscribe后调用 markForCheck()方法，重新启动检测;
      this.cdRef.markForCheck();
    });
  }
}
